package com.mathew.spring.appsp.controller;

import java.util.Date;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.mathew.spring.appsp.conf.Spa;
import com.mathew.spring.appsp.entity.User;
import com.mathew.spring.appsp.rep.UserRepository;

// import org.hibernate.annotations.common.util.impl.LoggerFactory;
// import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserController {
    // private static final Logger log = LoggerFactory.logger(UserController.class);

    @Autowired
    private UserRepository userRepository;

    @Resource
    private ApplicationContext ctx;

    @Autowired
    private Spa spa;

    @GetMapping()
    public String getPage(@RequestHeader HttpHeaders headers) {
        var jsonDesc =  JSON.toJSONString(ctx);
        var headerDesc = JSON.toJSONString(headers);
        return "spring" + headerDesc + jsonDesc;
    }

    @GetMapping("/all")
    public Iterable<User> getAllUsers() {
        return userRepository.findAll();
    }

    @GetMapping("/spa")
    public Spa getSpaConfig() {
        spa.setTime(new Date());
        return spa;
    }

    @GetMapping("/model")
    public Object model(Model model) {
        Map<String, Object> map = model.asMap();
        System.out.println(map);
        return map.get("info");
    }


}
